package com.server;

/**
 * @author Dmitry
 */
public class ByteMatrix {
    private final byte[] bytes;
    private final int x;
    private final int y;
    private final int width;
    private final int height;
    private final int subrectWidth;

    public ByteMatrix(byte[] bytes, int x, int y, int width, int height, int subrectWidth, int subrectHeight) {
        this.bytes = bytes;
        this.x = subrectWidth * x;
        this.y = subrectHeight * y;
        this.width = width;
        this.height = height;
        this.subrectWidth = subrectWidth;
    }

    public byte getValue(int index) {
        int ty = index / subrectWidth;
        int tx = index - ty * subrectWidth;
        return getValue(tx, ty);
    }

    public byte getValue(int x, int y) {
        int ty = (this.y + y);
        int tx = x + this.x;
        int offset = ty * width + tx;
        return bytes[ offset];
    }

}
